﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using IT_Tracker.ENTIDADES;
using IT_Tracker.DADOS;
using ComponentFactory.Krypton.Toolkit;
using System.Data.SqlClient;
using System.Collections;
using System.Globalization;


namespace IT_Tracker
{
    public partial class Form_TicketADD : KryptonForm
    {

     
        public Form_TicketADD(string forn, ArrayList cTipo, ArrayList cOperacao, Usuario sUser)
        {
            InitializeComponent();
            fornecedor = forn;
            comboTipo = cTipo;
            comboOper = cOperacao;
            sessionUser = sUser;            
            
        }

        private void Form_TicketADD_Load(object sender, EventArgs e)
        {
            cboTipo.DataSource = comboTipo;
            cboOperacao.DataSource = comboOper;
            cboOperacao.SelectedIndex = -1;
            cboTipo.SelectedIndex = -1;            
        }

        #region ***********************************************Declarações
        
        public string retorno;
        string fornecedor;       

        //Entidades/Classes
        Ticket tkt = new Ticket();
        Usuario sessionUser = new Usuario();                
        ArrayList comboTipo = new ArrayList();
        ArrayList comboOper = new ArrayList();

        //Metodos BD
        PersistenciaBDTicket bdTkt = new PersistenciaBDTicket();
        
        #endregion

        #region *****************************************************Validações
      
        public bool validarTextBox()
        {
            foreach (Control item in this.gboxTicket.Panel.Controls)
            {
                //checa textbox
                if (item.GetType() == typeof(KryptonTextBox))
                {
                    if (item.Text == "")
                    {
                        item.BackColor = Color.Red;
                        
                        return false;
                    }
                    else
                    {
                        item.BackColor = Color.White;
                    }
                }

                //checa combox
                if (item.GetType() == typeof(KryptonComboBox))
                {
                    if (item.Text == "")
                    {
                        item.BackColor = Color.Red;

                        return false;
                    }
                    else
                    {
                        item.BackColor = Color.White;
                    }

                }
            }
            return true;
        }

        private void txtTicket_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtChamado_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }


        #endregion

        #region ***********************************************Eventos Botões TextBox


        private void btnTxtTicket_Click_1(object sender, EventArgs e)
        {
            txtTicket.Clear();
        }

        private void btnTxtChamado_Click_1(object sender, EventArgs e)
        {
            txtChamado.Clear();
        }

        private void btntxtOperacaoDir_Click(object sender, EventArgs e)
        {
            if ((cboOperacao.Items.Count - 1) > cboOperacao.SelectedIndex)
            {
                cboOperacao.SelectedIndex++;
            }
        }

        private void btntxtOperacaoEsq_Click(object sender, EventArgs e)
        {
            if ((cboOperacao.SelectedIndex) >= 1)
            {
                cboOperacao.SelectedIndex--;
            }

        }

        private void btntxtAreaEsq_Click(object sender, EventArgs e)
        {
            if ((cboArea.SelectedIndex) >= 1)
            {
                cboArea.SelectedIndex--;
            }

        }

        private void btntxtAreaDir_Click(object sender, EventArgs e)
        {
            if ((cboArea.Items.Count - 1) > cboArea.SelectedIndex)
            {
                cboArea.SelectedIndex++;
            }


        }

        private void btntxtTipoEsq_Click(object sender, EventArgs e)
        {
            if ((cboTipo.SelectedIndex) >= 1)
            {
                cboTipo.SelectedIndex--;
            }

        }

        private void btntxtTipoDir_Click(object sender, EventArgs e)
        {
            if ((cboTipo.Items.Count - 1) > cboTipo.SelectedIndex)
            {
                cboTipo.SelectedIndex++;
            }

        }

        private void btntxtStatusEsq_Click(object sender, EventArgs e)
        {
            if ((cboStatus.SelectedIndex) >= 1)
            {
                cboStatus.SelectedIndex--;
            }

        }

        private void btntxtStatusDir_Click(object sender, EventArgs e)
        {
            if ((cboStatus.Items.Count - 1) > cboStatus.SelectedIndex)
            {
                cboStatus.SelectedIndex++;
            }

        }


        private void btnTxtPrevisao_Click(object sender, EventArgs e)
        {
            mtbPrevisao.Clear();
        }

        private void btnTxtDescricao_Click(object sender, EventArgs e)
        {
            txtDescricao.Clear();
        }

        private void btnTxtResumo_Click(object sender, EventArgs e)
        {
            rtxtResumo.Clear();
        }


        #endregion


        private void btnIncluir_Click(object sender, EventArgs e)
        {
            if (validarTextBox())
            {
                if (validarTicket(Convert.ToInt32(txtTicket.Text), fornecedor))
                {
                    tkt.ticket = Convert.ToInt32(txtTicket.Text);
                    tkt.Chamado = Convert.ToInt32(txtChamado.Text);
                    tkt.Descricao = txtDescricao.Text;
                    tkt.Operacao = cboOperacao.Text;
                    tkt.Status = cboStatus.Text;
                    tkt.Tipo = cboTipo.Text;
                    tkt.Area = cboArea.Text;
                    tkt.DataAbertura = Convert.ToDateTime(dtpAbertura.Text);
                    tkt.DataConclusao = Convert.ToDateTime("01/01/1900 00:00");
                    tkt.Resumo = "\r ====Criado Por: " + Convert.ToString(sessionUser.Nome) + " em " + DateTime.Now + "==== \r " + rtxtResumo.Text + "\r ";
                    tkt.Fornecedor = fornecedor;
                    tkt.DataAtualizacao = DateTime.Now;
                    tkt.Usuario = sessionUser.Nome;
                    tkt.CriadoPor = sessionUser.Nome;


                    //*****POG***** Para o caso do usuário não digitar a data de abertura e previsao
                    if (mtbPrevisao.Text != "  /  /")
                    {
                        tkt.DataPrevisao = Convert.ToDateTime(mtbPrevisao.Text);
                    }
                    else { tkt.DataPrevisao = Convert.ToDateTime("01/01/1900 00:00"); }
                    //****ENDPOG***

                    try
                    {
                        bdTkt.incluirTicket(tkt, "spTicketADD");
                        Form_Ticket.retorno = "** " + DateTime.Now.ToString() + " ** Ticket #" + txtTicket.Text + " incluído com sucesso!";
                        Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("** " + DateTime.Now.ToString() + " ** Error -> " + ex.Message);

                    }

                }

                else
                {
                    MessageBox.Show("** " + DateTime.Now.ToString() + " ** Error -> Ticket Já Existe");
                }
                
            }
            else
            {
                MessageBox.Show("Todos os campos devem ser preenchidos!");
            }
        }


        private void btnLimpar_Click(object sender, EventArgs e)
        {
            limpaForm();
        }

        private void limpaForm()
        {
            txtTicket.Clear();
            txtChamado.Clear();
            txtDescricao.Clear();
            rtxtResumo.Clear();
            mtbPrevisao.Clear();
            dtpAbertura.Value = DateTime.Now;
            cboArea.SelectedIndex = 0;
            cboOperacao.SelectedIndex = 0;
            cboStatus.SelectedIndex = 0;
            cboTipo.SelectedIndex = 0;
        }

        private Boolean validarTicket(int ticket, string forn)
        {
            Ticket t = bdTkt.GetTicket(ticket, forn, "spTicketLocaliza");
            if (t == null)
            {
                return true;
            }
            else { return false; }            
        }



      

 





    }
}
